////
////import com.hkbigdata.sybx.bean.ElderRegedit;
////import com.hkbigdata.sybx.service.ElderRegeditService;
////import org.springframework.beans.factory.annotation.Autowired;
////import org.springframework.web.bind.annotation.*;
////
/////**
//// * @author yangjing
//// * @creat 2025-05-06-14:59
//// * @email 3010335359@qq.com
//// */
////@RestController
////@RequestMapping("/api/elderRegedit")
////public class ElderRegeditController {
////    @Autowired
////    private ElderRegeditService elderRegeditService;
////
////    // 根据id查询单个记录
////    @GetMapping("/{user_id}")
////    public ElderRegedit getUserBeanByName(@PathVariable("user_id") String user_id) {
////        return elderRegeditService.getUserById(user_id);
////    }
////
////    // 插入记录
////    @PostMapping("/insert")
////    public String insertUser(@RequestBody ElderRegedit elderRegedit) {
////        try {
////            return elderRegeditService.insertUser(elderRegedit);
////        } catch (IllegalArgumentException e) {
////            return "error: " + e.getMessage();
////        }
////    }
////
////    // 根据id删除记录
////    @GetMapping("/delete/{user_id}")
////    public String deleteUserByName(@PathVariable("user_id") String user_id) {
////        return elderRegeditService.deleteUserById(user_id);
////    }
////
////    // 根据id修改记录
////    @PutMapping("/update")
////    public String updateUser(@RequestBody ElderRegedit elderRegedit) {
////        return elderRegeditService.updateUser(elderRegedit);
////    }
////}
////
////
//
//
//package com.hkbigdata.sybx.controller;
//
//import com.hkbigdata.sybx.bean.ElderRegedit;
//import com.hkbigdata.sybx.service.ElderRegeditService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.*;
//
///**
// * @author yangjing
// * @creat 2025-05-06-14:59
// * @email 3010335359@qq.com
// */
//@RestController
//@RequestMapping("/api/elderRegedit")
//@CrossOrigin(origins = "*") // 添加跨域支持
//public class ElderRegeditController {
//
//    @Autowired
//    private ElderRegeditService elderRegeditService;
//
//    // 根据id查询单个记录
//    @GetMapping("/{user_id}")
//    public ElderRegedit getUserBeanByName(@PathVariable("user_id") String user_id) {
//        return elderRegeditService.getUserById(user_id);
//    }
//
//    // 插入记录
//    @PostMapping("/insert")
//    public String insertUser(@RequestBody ElderRegedit elderRegedit) {
//        try {
//            System.out.println("[Controller] 接收到老人信息: " + elderRegedit);
//            return elderRegeditService.insertUser(elderRegedit);
//        } catch (IllegalArgumentException e) {
//            System.err.println("[Controller] 参数错误: " + e.getMessage());
//            return "error: " + e.getMessage();
//        } catch (Exception e) {
//            // 捕获所有异常，返回更详细的错误信息
//            System.err.println("[Controller] 插入失败: " + e.getMessage());
//            e.printStackTrace(); // 打印堆栈信息
//            return "error: 服务器内部错误，请联系管理员";
//        }
//    }
//
//    // 根据id删除记录
//    @GetMapping("/delete/{user_id}")
//    public String deleteUserByName(@PathVariable("user_id") String user_id) {
//        return elderRegeditService.deleteUserById(user_id);
//    }
//
//    // 根据id修改记录
//    @PutMapping("/update")
//    public String updateUser(@RequestBody ElderRegedit elderRegedit) {
//        return elderRegeditService.updateUser(elderRegedit);
//    }
//}


package com.hkbigdata.sybx.controller;

import com.hkbigdata.sybx.bean.ElderRegedit;
import com.hkbigdata.sybx.service.ElderRegeditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 老人信息管理控制器
 * @author yangjing
 */
@RestController
@RequestMapping("/api/elderRegedit")
public class ElderRegeditController {

    @Autowired
    private ElderRegeditService elderRegeditService;

    /**
     * 根据用户ID查询老人信息
     */
    @GetMapping("/{user_id}")
    public ResponseEntity<Map<String, Object>> getUserById(@PathVariable("user_id") String user_id) {
        Map<String, Object> result = new HashMap<>();
        try {
            ElderRegedit elderRegedit = elderRegeditService.getUserById(user_id);
            if (elderRegedit != null) {
                result.put("code", 200);
                result.put("message", "查询成功");
                result.put("data", elderRegedit);
                return ResponseEntity.ok(result);
            } else {
                result.put("code", 404);
                result.put("message", "未找到用户信息");
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(result);
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    /**
     * 插入老人信息
     */
    @PostMapping("/insert")
    public ResponseEntity<Map<String, Object>> insertUser(@RequestBody ElderRegedit elderRegedit) {
        Map<String, Object> result = new HashMap<>();
        try {
            System.out.println("[Controller] 接收到老人信息: " + elderRegedit);
            String insertResult = elderRegeditService.insertUser(elderRegedit);

            if ("插入成功".equals(insertResult)) {
                result.put("code", 200);
                result.put("message", "插入成功");
                return ResponseEntity.ok(result);
            } else {
                result.put("code", 400);
                result.put("message", insertResult);
                return ResponseEntity.badRequest().body(result);
            }
        } catch (IllegalArgumentException e) {
            result.put("code", 400);
            result.put("message", "参数错误: " + e.getMessage());
            return ResponseEntity.badRequest().body(result);
        } catch (Exception e) {
            System.err.println("[Controller] 插入失败: " + e.getMessage());
            e.printStackTrace();
            result.put("code", 500);
            result.put("message", "身份证号码或手机号码有误，请重新填写");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    /**
     * 根据用户ID删除老人信息
     */
    @DeleteMapping("/delete/{user_id}")
    public ResponseEntity<Map<String, Object>> deleteUserById(@PathVariable("user_id") String user_id) {
        Map<String, Object> result = new HashMap<>();
        try {
            String deleteResult = elderRegeditService.deleteUserById(user_id);
            result.put("code", 200);
            result.put("message", deleteResult);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "删除失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    /**
     * 更新老人信息
     */
    @PutMapping("/update")
    public ResponseEntity<Map<String, Object>> updateUser(@RequestBody ElderRegedit elderRegedit) {
        Map<String, Object> result = new HashMap<>();
        try {
            String updateResult = elderRegeditService.updateUser(elderRegedit);
            result.put("code", 200);
            result.put("message", updateResult);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "更新失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }
    /*
    * 查看全部老人信息
    * */
    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> listAllUsers() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<ElderRegedit> elderRegedits = elderRegeditService.getAllUser();
            result.put("code", 200);
            result.put("message", "查询成功");
            result.put("data", elderRegedits);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }
}